package org.example.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.example.common.R;
import org.example.pojo.entity.SysMenu;
import org.example.pojo.req.MenuReq;
import org.example.pojo.vo.MenuVo;
import org.example.service.SysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "后台菜单管理接口")
@RestController
@RequestMapping("/api/menu")
public class SysMenuController {


    @Autowired
    private SysMenuService sysMenuService;


    /**
     * 获取菜单tree
     *
     * @param menuName
     * @param status
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "menuName", value = ""),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "status", value = "")
    })
    @ApiOperation(value = "获取菜单tree", notes = "获取菜单tree", httpMethod = "GET")
    @GetMapping("/tree")
    public R getMenuList(@RequestParam(required = false) String menuName, @RequestParam(required = false) String status) {
        List<MenuVo> menus = sysMenuService.getList(menuName, status);
        return R.ok(menus);
    }

    /**
     * 添加菜单
     *
     * @param menuReq
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "MenuReq", name = "menuReq", value = "", required = true)
    })
    @ApiOperation(value = "添加改菜单", notes = "添加菜单", httpMethod = "POST")
    @PostMapping("/add")
    public R addMenu(@RequestBody MenuReq menuReq) {
        sysMenuService.addOrEditMenu(menuReq);
        return R.ok("添加成功");
    }

    /**
     * 编辑菜单
     * @param menuReq
     * @return
     */
    @PutMapping("/edit")
    public R editMenuInfo(@RequestBody MenuReq menuReq){
        sysMenuService.editMenuInfo(menuReq);
        return R.ok("编辑菜单");
    }

    /**
     * 删除菜单
     *
     * @param id
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "string", name = "id", value = "", required = true)
    })
    @ApiOperation(value = "删除菜单", notes = "删除菜单", httpMethod = "DELETE")
    @DeleteMapping("/remove/{id}")
    public R remove(@PathVariable String id) {
        return sysMenuService.removeMenu(id);
    }


}
